package com.Cychat.doctor.controller;

import com.Cychat.commons.entity.pojo.Appointment;
import com.Cychat.commons.entity.pojo.Drug;
import com.Cychat.commons.entity.vo.AppointmentDetailVo;
import com.Cychat.doctor.service.DrugService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.TreatmentRecord;
import com.Cychat.commons.entity.vo.TreatmentVo;
import com.Cychat.doctor.service.TreatmentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 诊疗记录Controller（适配前端接口）
 */
@RestController
@RequestMapping("/api/doctor/treatment")
public class TreatmentController {

    @Autowired
    private TreatmentRecordService treatmentRecordService;

    @Autowired
    private DrugService drugService;

    /**
     * 分页查询诊疗记录（前端调用：getTreatmentList）
     */
//    @GetMapping("/list")
//    public ResultMessage<IPage<TreatmentVo>> getTreatmentList(
//            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
//            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
//            @RequestParam("doctorId") Integer doctorId, // 从Pinia传过来的医生ID
//            @RequestParam("appointmentId") Integer appointmentId,
//            @RequestParam("keyword") String keyword
//    ) {
//        Page<TreatmentRecord> page = new Page<>(pageNum, pageSize);
//        IPage<TreatmentVo> treatmentPage = treatmentRecordService.getTreatmentVoPage(page, doctorId, appointmentId, keyword);
//        return ResultMessage.success(treatmentPage);
//    }

    /**
     * 获取诊疗详情（前端调用：getTreatmentInfo）
     */
//    @GetMapping("/info/{treatmentId}")
//    public ResultMessage<TreatmentVo> getTreatmentInfo(@PathVariable("treatmentId") Integer treatmentId) {
//        TreatmentVo treatmentVo = treatmentRecordService.getTreatmentVoById(treatmentId);
//        return treatmentVo != null ? ResultMessage.success(treatmentVo) : ResultMessage.error("诊疗记录不存在");
//    }

    /**
     * 新增诊疗记录（前端调用：addTreatment）
     */
    @PostMapping("/add")
    public ResultMessage<Boolean> addTreatment(@RequestBody TreatmentRecord treatment) {
        try {
            boolean result = treatmentRecordService.saveTreatment(treatment);
            return ResultMessage.success(result);
        } catch (IllegalArgumentException e) {
            return ResultMessage.error(e.getMessage());
        } catch (Exception e) {
            return ResultMessage.error("新增诊疗记录失败");
        }
    }

    /**
     * 编辑诊疗记录（前端调用：editTreatment）
     */
    @PostMapping("/edit")
    public ResultMessage<Boolean> editTreatment(@RequestBody TreatmentRecord treatment) {
        try {
            boolean result = treatmentRecordService.updateTreatment(treatment);
            return ResultMessage.success(result);
        } catch (IllegalArgumentException e) {
            return ResultMessage.error(e.getMessage());
        } catch (Exception e) {
            return ResultMessage.error("编辑诊疗记录失败");
        }
    }

    /**
     * 删除诊疗记录（前端调用：deleteTreatment）
     */
    @DeleteMapping("/delete/{treatmentId}")
    public ResultMessage<Boolean> deleteTreatment(@PathVariable("treatmentId") Integer treatmentId) {
        boolean result = treatmentRecordService.removeById(treatmentId);
        return result ? ResultMessage.success(result) : ResultMessage.error("删除诊疗记录失败");
    }

    /**
     * 获取药品列表（前端下拉选择用：getDrugList）
     */
    @GetMapping("/drug/list")
    public ResultMessage<List<Drug>> getDrugList() {
        List<Drug> drugList = drugService.list(new LambdaQueryWrapper<Drug>());
        return ResultMessage.success(drugList);
    }

    /**
     * 获取医生的预约列表（前端下拉选择用：getAppointmentList）
     */
    @GetMapping("/appointment/list")
    public ResultMessage<IPage<AppointmentDetailVo>> getAppointmentList(
            @RequestParam("doctorId") Integer doctorId,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<Appointment> page = new Page<>(pageNum, pageSize);
        IPage<AppointmentDetailVo> appointmentPage = treatmentRecordService.getDoctorAppointmentPage(page, doctorId);
        return ResultMessage.success(appointmentPage);
    }

    /**
     * 查询当前医生的所有诊疗记录（全量，不分页，前端处理分页搜索）
     * @param doctorId 医生ID（从请求头获取，避免前端传参）
     * @return 诊疗VO列表
     */
    @GetMapping("/list")
    public ResultMessage<List<TreatmentVo>> getDoctorTreatmentList(@RequestHeader("doctorId") Integer doctorId) {
        List<TreatmentVo> list = treatmentRecordService.getDoctorTreatmentList(doctorId);
        return ResultMessage.success(list);
    }

    /**
     * 查询诊疗详情（含权限校验）
     * @param treatmentId 诊疗ID
     * @param doctorId 医生ID（请求头）
     * @return 诊疗详情VO
     */
    @GetMapping("/info/{treatmentId}")
    public ResultMessage<TreatmentVo> getTreatmentDetail(
            @PathVariable("treatmentId") Integer treatmentId,
            @RequestHeader("doctorId") Integer doctorId
    ) {
        TreatmentVo vo = treatmentRecordService.getDoctorTreatmentDetail(treatmentId, doctorId);
        return vo != null ? ResultMessage.success(vo) : ResultMessage.error("诊疗记录不存在或无权限");
    }
}